<?php

	header("Content-Type:text/html; charset=utf-8");

	/***********************用户注册模块*********************/
	session_start();
	//连接数据库 (引入)
	include("connect_db.php");

	// $phone = mysql_real_escape_string($_REQUEST['phone']);

	$phone = $_REQUEST['phone'];  //从表单中获取到的数
	$nickname = $_REQUEST['nickname'];
	$password = md5($_REQUEST['pwd']);
	$sex = $_REQUEST['sex'];
	$age = $_REQUEST['age'];
	$address = $_REQUEST['address'];

	//当输入的用户不为空时
	if($phone){
		//数据库查询
		$selectResult = mysql_query("SELECT * FROM user");
		$exits = false;
		while($row =mysql_fetch_array($selectResult)){
			if($phone == $row['phone']){// 若果存在
				$exits = true;
				// $register_res = array("register_res"=>false,"register_code"=>0,"register_msg"=>"此用户已存在");
				$arr = array("description"=>"此用户已被注册","code"=>0); //0表示用户已经被注册可以注册 ， 1表示此用户可以注册
				$json = array("data"=>$arr,"errcode"=>0,"errmsg"=>"error");

				$data = json_encode($json);
				echo $data;
			}
		}
		
		if(!$exits){//否则  不存在 插入到数据库中

			$sql = "INSERT INTO user(phone,nickname,pwd,sex,age,address) VALUES(".$phone.",'".$nickname."','".$password."','".$sex."','".$age."','".$address."')";

			$success = mysql_query($sql);
			if($success){//数据插入成功
				$arr = array("description"=>"注册成功","code"=>1);
				$json = array("data"=>$arr,"errcode"=>1,"errmsg"=>"ok");
				$data = json_encode($json);
				echo $data;
			}else{//注册失败
				$arr = array("description"=>"注册失败","code"=>1);
				$json = array("data"=>$arr,"errcode"=>-1,"errmsg"=>"error");
				$data = json_encode($json);
				echo $data;
			}
		}
			
	}else{// 提示用户不能为空
		$arr = array("description"=>"用户输入不能为空");
		$json = array("data"=>$arr,"errcode"=>-2,"errmsg"=>"error");
		$data = json_encode($json);
		echo $data;
	}

	// 关闭数据库连接
	mysql_close($link);

?>